import Vue from 'vue'
import App from './App.vue'
import router from './router.js'
import jv from './api/enums.js'
import axios from 'axios'
// import ElementUI from 'element-ui'
import ElementUIExt from 'element-ui-ext'
import './element-variables.scss'
import store from "./view/store"
// import VueQuillEditor from 'vue-quill-editor'

jv.system = "admin";

(function (jv) {
    window.jv = jv;

    jv.initVue({vue: Vue, axios: axios, router: router});
    Vue.config.productionTip = true;

    jv.reset = function () {
    };

    jv.debugger = function (v) {
        console.log(v);
    };



    Object.defineProperty(Vue.prototype, "viewer", {
        value() {
            this.$nextTick(() => {
                this.$RecursionVNode(vue => {
                    if (!vue.$vnode) {
                        return;
                    }

                    var tag = vue.$vnode.componentOptions.tag;
                    if (tag) {
                        if (tag == "el-input") {
                            vue.readonly = true;
                            return false;
                        }

                        if (tag == "upload") {
                            vue.readOnly = true;
                            return false;
                        }

                        if (tag == "el-cascader") {
                            vue.disabled = true;
                            return false;
                        }
                    }
                })
            });
        }
    });


    Object.defineProperty(jv, "token", {
        get() {
            return window.localStorage.getItem(jv.system + '-token') || "";
        },
        set(value) {
            window.localStorage.setItem(jv.system + '-token', value || "");
        },
        enumerable: false
    });
    axios.interceptors.request.use(config => {
        var token = jv.token;
        if (token) {
            config.headers.post["token"] = token;
            config.headers.get["token"] = token;
        }
        return config;
    }, error => {
        return Promise.reject(error);
    });


    axios.interceptors.response.use(response => {
        try {
            var token = response.headers["token"];
            if (token) {
                jv.token = token;
            }
        } catch (e) {
            console.error("axios.interceptors.response:" + e.message)
            return Promise.reject(e);
        }
        return response;
    }, error => {
        var resp = error.response;
        if (resp) {
            var status = resp.status;
            if (status == 401) {
                jv.main.loginFromVisible = true;
                return Promise.reject(error);
            }
        }
        return Promise.reject(error);
    });

// Vue.use(VueQuillEditor);
    /*Vue.config.productionTip = false;*/
// Vue.use(ElementUI);
    Vue.use(ElementUIExt);

    Vue.filter("gb", value => {
            return (value / 1024).ToRound() + "GB";
        }
    );

})
(jv);


(function (jv) {

    jv.Vue.mixin({
        mounted() {
            var tagName = this.$vnode && this.$vnode.componentOptions.tag;
            if (!tagName) {
                return;
            }
            //有的时候不灵，很奇怪。
            if (tagName == "el-button") {
                if (!this.size && this.$el) {
                    this.$el.classList.add("el-button--mini");
                }
            }
        }
    });

    var vue = new Vue({
        router,
        store,
        /*        watch: {
                    '$route'(to, from) {
                        jv.loading = false;
                    }
                },*/
        render: h => h(App)
    }).$mount('#app')

    vue.$nextTick(it => {
        // jv.main.loadData();
        document.documentElement.classList.add("loaded");
    });

})(jv);
